﻿using System.Data;
using System.Data.SQLite;
using System.Text;
using NDK.Module.Model;
using NDK.Utils;

namespace NDK.Module.DAL
{
    class StringParamServices
    {
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return SQLiteUtils.GetMaxID("ID", "StringParams");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from StringParams");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = ID;

            return SQLiteUtils.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(StringParam model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into StringParams(");
            strSql.Append("ID,参数名称,字符串值)");
            strSql.Append(" values (");
            strSql.Append("@ID,@参数名称,@字符串值)");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4),
					new SQLiteParameter("@参数名称", DbType.String,50),
					new SQLiteParameter("@字符串值", DbType.String,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.参数名称;
            parameters[2].Value = model.字符串值;

            SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(StringParam model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update StringParams set ");
            strSql.Append("参数名称=@参数名称,");
            strSql.Append("字符串值=@字符串值");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@参数名称", DbType.String,50),
					new SQLiteParameter("@字符串值", DbType.String,50),
					new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = model.参数名称;
            parameters[1].Value = model.字符串值;
            parameters[2].Value = model.ID;

            int rows = SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StringParams ");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = ID;

            int rows = SQLiteUtils.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from StringParams ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = SQLiteUtils.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public StringParam GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,参数名称,字符串值 from StringParams ");
            strSql.Append(" where ID=@ID ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = ID;

            StringParam model = new StringParam();
            DataSet ds = SQLiteUtils.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.参数名称 = ds.Tables[0].Rows[0]["参数名称"].ToString();
                model.字符串值 = ds.Tables[0].Rows[0]["字符串值"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public StringParam GetModel(string Name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,参数名称,字符串值 from StringParams ");
            strSql.Append(" where 参数名称=@Name ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@Name", DbType.String,50)};
            parameters[0].Value = Name;

            StringParam model = new StringParam();
            DataSet ds = SQLiteUtils.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.参数名称 = ds.Tables[0].Rows[0]["参数名称"].ToString();
                model.字符串值 = ds.Tables[0].Rows[0]["字符串值"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,参数名称,字符串值 ");
            strSql.Append(" FROM StringParams ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLiteUtils.Query(strSql.ToString());
        }


        #endregion  Method
    }
}
